package cn.itsource.basic.service.impl;

import cn.itsource.basic.mapper.BaseMapper;
import cn.itsource.basic.query.BaseQuery;
import cn.itsource.basic.service.IBaseService;
import cn.itsource.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import java.util.List;

/**
 * 业务实现类：基础
 * @param <T>
 */
// 我这个service 是干嘛的？就是装了一个 公共的方法 根本不需要spring管理
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {

    @Autowired
    private BaseMapper<T> baseMapper;

    @Override
    public T findById(Long id) {
        return baseMapper.findById(id);
    }

    @Override
    public List<T> findAll() {
        return baseMapper.findAll();
    }

    @Override
    @Transactional
    public void add(T t) {
        baseMapper.add(t);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        baseMapper.delete(id);

    }

    @Override
    @Transactional
    public void batchDelete(List<Long> ids) {
        baseMapper.batchDelete(ids);

    }

    @Override
    @Transactional
    public void update(T t) {
        baseMapper.update(t);
    }

    @Override
    public PageList<T> queryPage(BaseQuery query) {
        // 去除 keyword的前后空格
        if (!StringUtils.isEmpty(query.getKeyword())) {
            query.setKeyword(query.getKeyword().trim());
        }
        // 查询数量
        Integer totals = baseMapper.queryCount(query);
        // 查询数据
        List<T> data = baseMapper.queryData(query);
        // 代码的健壮性不够 剩下的东西你们自己补 你们要 判断是否为空！

        return new PageList<T>(data, totals);
    }
}